package com.example.springsecurity.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author wuancheng
 * @date 2024/6/19 18:12
 * @description 统一返回
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> implements Serializable {
    private static final long serialVersionUID = -7383207803321137482L;
    private T data;
    private String resultCode;
    private String errorCode;
    private String errorDesc;

    private static final String SUCCESS = "1";

    private static final String FAIL = "0";

    public static CommonResult succeed() {
        return succeedWith(null, SUCCESS);
    }

    public static <T> CommonResult succeed(T data) {
        return succeedWith(data, SUCCESS);
    }

    public static <T> CommonResult succeedWith(T data, String resultCode) {
        return new CommonResult<T>(data, resultCode, null, null);
    }

    public static CommonResult failed(String errorCode, String errorDesc) {
        return failedWith(null, errorCode, errorDesc);
    }

    public static <T> CommonResult failedWith(T data, String errorCode, String errorDesc) {
        return new CommonResult(data, FAIL, errorCode, errorDesc);
    }

    public CommonResult success() {
        this.setResultCode(SUCCESS);
        return this;
    }

    public CommonResult<T> success(T data) {
        this.setResultCode(SUCCESS);
        this.setData(data);
        return this;
    }
}

